package com.sky.mapper;

import com.sky.entity.AddressBook;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface AddressBookMapper {
    //新增地址
    @Insert("insert into address_book" +
            "        (user_id, consignee, phone, sex, province_code, province_name, city_code, city_name, district_code," +
            "         district_name, detail, label, is_default)" +
            "        values (#{userId}, #{consignee}, #{phone}, #{sex}, #{provinceCode}, #{provinceName}, #{cityCode}, #{cityName}," +
            "                #{districtCode}, #{districtName}, #{detail}, #{label}, #{isDefault})")
    void insertAddress(AddressBook addressBook);

    //查询用户所有地址
    @Select("select * from address_book where user_id=#{userId}")
    List<AddressBook> allAddress(Long userId);

    //查询用户默认地址
    @Select("select * from address_book where user_id=#{userId} and is_default=1")
    AddressBook defaultAddress(Long userId);

    //修改地址
    void updateAddress(AddressBook addressBook);

    //根据id删除地址
    @Delete("delete from address_book where id=#{id}")
    void deleteAddress(Long id);

    //根据id查询地址
    @Select("select * from address_book where id=#{id}")
    AddressBook addressById(Long id);

    //设置是否默认地址
    @Update("update address_book set is_default=#{isDefault} where id=#{id}")
    void setDefaultAddress(AddressBook addressBook);
}
